package com.b2b.util;

import com.alibaba.fastjson.JSON;
import com.b2b.model.MessageDto;
import com.b2b.model.SystemRoleDto;
import com.b2b.model.entity.Message;
import com.b2b.model.receive.MyMessage;
import com.b2b.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class MessageUtil {
    private final MessageService messageService;
    private final CompanyInfoUtil companyInfoUtil;

    @Autowired
    public MessageUtil(MessageService messageService,CompanyInfoUtil companyInfoUtil){
        this.messageService = messageService;
        this.companyInfoUtil = companyInfoUtil;
    }


    public int insertMessage(MessageDto messageDto){
        Message message = getMessage(messageDto);
        message.setSendTime(new Date());
        int count = messageService.insert(message);
        if (count<=0){
            return count;
        }else {
            count = message.getMessageId();
            return count;
        }
    }
    public List<MyMessage> selectMyMessage(Integer companyId){
        List<MyMessage> messageList = new ArrayList<MyMessage>();
        List<Integer> receiverIdList = getByC(companyId);
        for (int i = 0; i < receiverIdList.size(); i++) {
            Integer receiverId = receiverIdList.get(i);
            List<Message> messageList1 = getById(companyId,receiverId);
            List<Message> messageList2 = getById(receiverId,companyId);
            Message message1 = messageList1.get(messageList1.size()-1);
            Message message2 = messageList2.get(messageList2.size()-1);
            Message lastMessage = new Message();
            if (message1.getSendTime().compareTo(message2.getSendTime())>=0){
                lastMessage = message1;
            }else {
                lastMessage = message2;
            }
            MyMessage myMessage = new MyMessage();
            myMessage.setSenderId(lastMessage.getSenderId());
            myMessage.setReceiverId(lastMessage.getReceiverId());
            myMessage.setSenderName(companyInfoUtil.getCompanyName(lastMessage.getSenderId()));
            myMessage.setReceiverName(companyInfoUtil.getCompanyName(lastMessage.getReceiverId()));
            myMessage.setLastContent(lastMessage.getContent());
            myMessage.setSendTime(lastMessage.getSendTime());

            messageList.add(myMessage);
        }

        return messageList;
    }

    public List<Message> getMessages(Integer senderId,Integer receiverId){
        Message message = new Message();
        List<Integer> relation = new ArrayList<>();
        if (senderId>receiverId){
            relation.add(receiverId);
            relation.add(senderId);
        }else {
            relation.add(senderId);
            relation.add(receiverId);
        }
        message.setRelation(JSON.toJSONString(relation));
        List<Message> messages = messageService.select(message);
        return messages;
    }
    public List<Message> getById(Integer senderId,Integer receiverId){
        Message message = new Message();
        message.setSenderId(senderId);
        message.setReceiverId(receiverId);
        List<Message> messageList = messageService.select(message);
        return messageList;
    }

    public List<Integer> getByC(Integer companyId){
        List<Integer> receiverId = new ArrayList<Integer>();
        List<Message> messageList1 = getByIdFromS(companyId);
        for (int i = 0; i < messageList1.size(); i++) {
            Message message = messageList1.get(i);
            receiverId.add(message.getReceiverId());
        }

        List<Message> messageList2 = getByIdFromR(companyId);
        for (int i = 0; i < messageList2.size(); i++) {
            Message message = messageList2.get(i);
            receiverId.add(message.getSenderId());
        }

        List<Integer> receiverIdList = receiverId.stream().distinct().collect(Collectors.toList());

        return receiverIdList;
    }
    public List<Message> getByIdFromS(Integer senderId){
        Message message = new Message();
        message.setSenderId(senderId);
        List<Message> messageList = messageService.select(message);
        return messageList;
    }
    public List<Message> getByIdFromR(Integer receiverId){
        Message message = new Message();
        message.setReceiverId(receiverId);
        List<Message> messageList = messageService.select(message);
        return messageList;
    }
    public Message getMessage(MessageDto messageDto){
        Message message = new Message();
        message.setMessageId(messageDto.getMessageId());
        message.setSenderId(messageDto.getSenderId());
        message.setReceiverId(messageDto.getReceiverId());
        message.setContent(messageDto.getContent());
        message.setSendTime(messageDto.getSendTime());
        List<Integer> relation = new ArrayList<>();
        if (messageDto.getSenderId()>messageDto.getReceiverId()){
            relation.add(messageDto.getReceiverId());
            relation.add(messageDto.getSenderId());
        }else {
            relation.add(messageDto.getSenderId());
            relation.add(messageDto.getReceiverId());
        }
        message.setRelation(JSON.toJSONString(relation));
        return message;
    }
}
